/**
 * Created with IntelliJ IDEA.
 * User: 卞京帅{bjslogin@gmail.com}
 * Date: 12-11-14
 * Time: 下午8:08
 * 主页的框架controller
 */
Ext.define('Airways.controller.system.FrameController', {
    extend:'Ext.app.Controller',
    views:['system.FrameView'],
    refs:[
        {ref:'frameLeftView',selector:'frameLeftView'},
        {ref:'frameCenterView',selector:'frameCenterView'},
        {ref:'frameTopView',selector:'frameTopView'},
        {ref:'frameView',selector:'frameView'}
    ],
    init:function() {
        this.control({
            'frameLeftView':{
                afterrender:function(){
                    var controller = this;
                    callAjaxLocal(basePath+"system/operation/getTopLevel",{},function(result){
                        var items = [];
                        for(var i = 0 ; i < result.length ; i ++ ){
                            var treePanel = Ext.create('Ext.tree.Panel',{
                                title:result[i].text,
                                autoHeight:true,
                                autoScroll: true,
                                split: true,
                                rootVisible:false,
                                lines:true,
                                store: Ext.create('Ext.data.TreeStore', {
                                    root: {
                                        expanded: true,
                                        children: result[i].children
                                    },
                                    sortable:true,
                                    sorters: [{
                                        property: 'menuIndex',
                                        direction: 'ASC'
                                    }]
                                }),
                                listeners:{
                                    'itemclick':function(view,record){
                                        if(!record.data.leaf) return;
                                        if(Ext.getCmp(record.raw.id)){
                                            Ext.getCmp(record.raw.id).show();
                                        }else{
                                            /*var loadMask = new Ext.LoadMask(Ext.getBody(), {msg:"页面加载中,请稍候..."});
                                            loadMask.show();*/
                                            //采用Airways droptip 提示方式
                                            Ext.DropTip.msg('提示',"《" + record.raw.menuName + '》页面加载中,请稍候...');
                                            try {
                                                var menu_controller = record.raw.menuController;
                                                if (!Ext.ClassManager.isCreated(menu_controller)) {
                                                    var init = controller.application.getController(menu_controller);
                                                    init.init();
                                                }
                                                controller.getFrameCenterView().add(
                                                    Ext.create(record.raw.menuView, {
                                                        id:record.raw.id,
                                                        closable:true,
                                                        closeAction:'destroy',
                                                        iconCls:record.raw.iconCls,
                                                        title:record.raw.menuName/*,
                                                        listeners:{
                                                            afterrender:function () {
                                                                loadMask.hide();
                                                            }
                                                        }*/
                                                    })
                                                ).show();
                                            } catch (e) {
                                                Ext.Msg.show({title:'提示',msg:'《' + record.raw.menuName + '》页面错误,加载失败!',icon:Ext.Msg.ERROR,buttons:Ext.Msg.OK});
                                                throw e;
                                            }
                                        }
                                    },
                                    'itemexpand':function(node,eOpts){
                                        node.set({iconCls:node.raw.expandedCls});
                                    },
                                    'itemcollapse':function(node,eOpts){
                                        node.set({iconCls:node.raw.collapsedCls});
                                    }

                                }
                            });
                            items.push(treePanel);
                        }
                        controller.getFrameLeftView().add(items);
                    });
                }
            },
            'frameTopView button[action=green]':{
                click:function(){
                    this.changeCSS("extjs/resources/css/ext-all.css");
                }
            },
            'frameTopView button[action=access]':{
                click:function(){
                    this.changeCSS("extjs/resources/css/ext-all-access.css");
                }
            },
            'frameTopView button[action=gray]':{
                click:function(){
                    this.changeCSS("extjs/resources/css/ext-all-gray.css");
                }
            },
            'frameTopView button[action=password]':{
                click:function(){
                    alert('changePassword');
                }
            },
            'frameTopView button[action=logout]':{
                click:function(){
                    Ext.MessageBox.confirm("提示","确定注销登录么",function (e){
                        if( e == "yes"){
                            location.href = basePath+'logout';
                        }
                    });
                }
            }
        });
    },
    changeCSS:function(url){
        Ext.util.CSS.swapStyleSheet("extjs4_css",url);
        var time = new Date();
        time.setTime(time.getTime()+365*24*60*60*1000);
        document.cookie = "css=" + url + "; expires=" + time.toGMTString();
    }
});
